package com.duanjw.springboot.docker;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Docker 应用
 * <p>
 * 1. 首先电脑需要安装 Docker。
 * 2. 执行根目录下的 run.sh，会把应用打包成可执行 jar，并把该 jar 制作成 docker 镜像，最后用镜像启动容器。
 * 3. 启动成功后可通过浏览器访问： http://localhost:8080
 *
 * @author duanjw
 * @since 2019-04-16
 */
@EnableAutoConfiguration
@RestController
public class DockerApplication {

    final Logger log = LoggerFactory.getLogger(DockerApplication.class);

    /**
     * 程序入口
     *
     * @param args
     */
    public static void main(String[] args) {
        // 引导应用程序启动spring，同时会启动Tomcat
        SpringApplication.run(DockerApplication.class, args);
    }

    @RequestMapping
    public Map script(HttpServletRequest request, @Nullable @RequestBody Map map, @Nullable @RequestParam String ids) {
        Logger log = LoggerFactory.getLogger(DockerApplication.class);
        Map<String, String[]> parameterMap = request.getParameterMap();
        log.info("=============请求参数 start=============");
        parameterMap.forEach((key, value) -> {
            log.info("请求参数 key：{}，value：{}", key, value);
        });
        Enumeration<String> headerNames = request.getHeaderNames();
        log.info("=============请求参数 end=============");

        log.info("=============请求头 start=============");
        while (headerNames.hasMoreElements()) {
            String headerKey = headerNames.nextElement();
            String headerValue = request.getHeader(headerKey);
            log.info("请求头 key：{}，value：{}", headerKey, headerValue);
        }
        log.info("=============请求头 end=============");
        log.info("=============请求 body start=============");

        if (map != null) {
            map.forEach((key, value) -> {
                log.info("body 中请求参数 key：{}，value：{}", key, value);
            });
        } else {
            log.info("body 中请求参数为空");
        }
        log.info("=============请求 body end=============");
        Map m = new HashMap();
        m.put("code", 0);
        m.put("data", getObjects(ids));
        m.put("message", "");
        return m;
    }

    private ArrayList<Object> getObjects(String ids) {
        ArrayList<Object> objects = new ArrayList<>();

        HashMap<Object, Object> allMap = new HashMap<>();
        allMap.put("41111", "韩大,17319725620,2966396945@qq.com,oSOHi6LqiziC81sl1Nk8et_XOo2U");
        allMap.put("41112", "杨三,17319725620,2966396945@qq.com,oSOHi6LqiziC81sl1Nk8et_XOo2U");
        allMap.put("41113", "何四,18630070626,554343346@qq.com,oSOHi6Cz0YRiYCL7d7UBUxtywUIk");
        if(ids != null ){
            String[] split = ids.split(",");
            for (String id : split) {
                Object o = allMap.get(id);
                if (o != null) {
                    HashMap<Object, Object> resultMap = new HashMap<>();
                    resultMap.put("id", id);
                    String[] split1 = o.toString().split(",");
                    resultMap.put("name", split1[0]);
                    resultMap.put("phone", split1[1]);
                    resultMap.put("email", split1[2]);
                    resultMap.put("wechatServiceNum", split1[3]);
                    objects.add(resultMap);
                }
            }
        } else {
            allMap.forEach((key, value) -> {
                HashMap<Object, Object> resultMap = new HashMap<>();
                resultMap.put("id", key);
                String[] split1 = value.toString().split(",");
                resultMap.put("name", split1[0]);
                resultMap.put("phone", split1[1]);
                resultMap.put("email", split1[2]);
                resultMap.put("wechatServiceNum", split1[3]);
                objects.add(resultMap);
            });
        }

        return objects;
    }

    @RequestMapping("1")
    public List script1(HttpServletRequest request, @Nullable @RequestBody Map map) {
        Logger log = LoggerFactory.getLogger(DockerApplication.class);
        Map<String, String[]> parameterMap = request.getParameterMap();
        log.info("==========================");
        parameterMap.forEach((key, value) -> {
            log.info("请求参数 key：{}，value：{}", key, value);
        });
        if (map != null) {
            map.forEach((key, value) -> {
                log.info("body 中请求参数 key：{}，value：{}", key, value);
            });
        } else {
            log.info("body 中请求参数为空");
        }
        log.info("==========================");
        HashMap<Object, Object> resultMap = new HashMap<>();
        resultMap.put("id", 1);
        resultMap.put("name", "脚本选人");
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(resultMap);
        return objects;
    }
}
